package com.auxgroup.hr.front.site.co.process;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.Valid;

import com.auxgroup.hr.front.dao.domain.*;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import com.auxgroup.hr.front.service.process.*;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfTerHdrRespVO;
import com.auxgroup.hr.front.site.vo.process.WfTerHdrSaveQueVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.vo.AuthorityFiledVO;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.service.comm.SysDictService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfTerHdrRespVO;
import com.auxgroup.hr.front.site.vo.process.WfTerHdrSaveQueVO;
import com.auxgroup.hr.front.ws.ProcessApproveQueDTO;

/**
 * 离职申请流程CO
 * 
 * @author wangchangyun
 * @version 创建时间：180527
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfterhdr")
public class WfTerHdrCO extends BaseWfCO {

	private static final Logger logger = LoggerFactory.getLogger(WfTerHdrCO.class);

	@Autowired
	public WfTerHdrService  wfTerHdrService;
	@Autowired
	private PersonalDataService personalDataService;


	@GetMapping(value="/getWfTerHdr")
	public BaseResponseVO getWfTerHdr(String cTransactionNbr,String stepNbr,@SessionGain("user") UserDTO user){
		if(StringUtils.isBlank(cTransactionNbr)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
		}
		PsCWfTerHdr terHdr =  wfTerHdrService.getWfTerHdr(cTransactionNbr);
		if(terHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}
		WfTerHdrRespVO respVO = objectMapper.map(terHdr, WfTerHdrRespVO.class);
		respVO.setTerReason(terHdr.terReason());
		//导师名字
		PsCPersonalData daoshi  = personalDataService.getPersonalData(terHdr.getcEmplid2());
		if(daoshi != null) {
			respVO.setcEmplidName(daoshi.getName());
		}
		List<Object[]> resons =sysDictService.getActionResons(WfTypeEnum.WF_STYPE_TER.getType());
		for (Object[] str : resons) {
			if(str[0].equals(respVO.getActionReason())){
				respVO.setActionReasonValue(String.valueOf(str[1]));
				break;
			}
		}

		if(StringUtils.isNotEmpty(stepNbr)) {
			fillBaseWfInfo(user, terHdr, stepNbr, respVO);
		} else {
			fillBaseWfInfo(user, terHdr, respVO);//FIXME 授权字段有部分需要特殊处理
		}

		return buildRespone(respVO);
	}
	
	/**
	 * 保存离职审批内容并审批流程
	 * @param requestVO
	 * @param binding
	 * @return
	 */
	@PostMapping(value="/saveTer")
	public BaseResponseVO saveWfTerHdr(@Valid WfTerHdrSaveQueVO requestVO, BindingResult binding, @SessionGain("user") UserDTO user){
		logger.info("PrcHdr save param:{}",requestVO);
		// 表单校验
        super.assertValidation(binding);
        PsCWfTerHdr terHdr = wfTerHdrService.getWfTerHdr(requestVO.getcTransactionNbr());
        if(terHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}

		//设置数据
		Set<String> fs = super.getEditableFields(terHdr, user);
		if(fs.contains("cLastWorkDt"))terHdr.setcLastWorkDt(requestVO.getcLastWorkDt());
		if(fs.contains("actionReason"))terHdr.setActionReason(requestVO.getActionReason());
		if(fs.contains("cTerDetail"))terHdr.setcTerDetail(requestVO.getcTerDetail());
		if(fs.contains("cTerResnDescr"))terHdr.setcTerResnDescr(requestVO.getcTerResnDescr());
		//保存数据
		wfTerHdrService.save(terHdr);
		//审批流程
		return requestWs(WfTypeEnum.WF_STYPE_TER.getType(), requestVO, user);
	}


	@Override
	protected Set<String> getAuthFileds() {
		return new HashSet<>(Arrays.asList("cTerIsJk", "cTerIsJthk",
				"cTerIsPxxy","cTerIsSfyhbary", "cTerIsQsjyxy",
				"cTerIsAjf", "cTerIsAjfje", "cTerIsGjgw",

                "cLastWorkDt", "actionReason", "cTerDetail", "cTerResnDescr"));
	}
}
